use std::collections::HashMap;

use error_chain::error_chain;
use reqwest::Client;
use reqwest::header::{AUTHORIZATION, USER_AGENT};
use serde::Deserialize;
use url::Url;

#[derive(Deserialize, Debug)]
pub struct HeadersEcho {
    pub headers: HashMap<String, String>,
}

error_chain! {
    foreign_links {
        Reqwest(reqwest::Error);
        UrlParse(url::ParseError);
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let url = Url::parse_with_params("http://httpbin.org/headers",
                                     &[("lang", "rust"), ("browser", "servo")])?;

    let mut response = Client::new()
        .get(url)
        .header(USER_AGENT, "Rust-test")
        .header(AUTHORIZATION, "Bearer DEadBEEfc001cAFeEDEcafBAd")
        .header("X-Powered-By", "Guybrush Threepwood")
        .send().await?;

    assert_eq!((&response).url().as_str(),
               "http://httpbin.org/headers?lang=rust&browser=servo");

    let out: HeadersEcho = response.json().await?;
    assert_eq!(out.headers["Authorization"],
               "Bearer DEadBEEfc001cAFeEDEcafBAd");
    assert_eq!(out.headers["User-Agent"], "Rust-test");
    assert_eq!(out.headers["X-Powered-By"], "Guybrush Threepwood");


    println!("{:?}", out);
    Ok(())
}
